package com.daocao.ds;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.KeyEvent;
import android.widget.Toast;

import com.daocao.ds.api.Api;
import com.daocao.ds.base.BaseActivity;
import com.daocao.ds.event.ChangPageEvent;
import com.daocao.ds.event.UserEvent;
import com.daocao.ds.event.UserNoDataEvent;
import com.daocao.ds.fragment.HomeFragment;
import com.daocao.ds.fragment.HomeOrderFragment;
import com.daocao.ds.fragment.MyFragment;
import com.daocao.ds.fragment.MyOrderFragment;
import com.daocao.ds.util.Network;
import com.daocao.ds.util.RxLifeUtil;
import com.daocao.ds.util.ToastUtil;
import com.daocao.ds.view.BussinessBottomLayout;
import com.daocao.ds.view.HomeViewPager;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

public class MainActivity extends BaseActivity implements BussinessBottomLayout.OnChooseListener {

    @BindView(R.id.viewpager)
    HomeViewPager viewpager;
    @BindView(R.id.bottom_view)
    BussinessBottomLayout bottomView;
    private Unbinder unbiner;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initBar();
        unbiner = ButterKnife.bind(this);

        Fragment[] fragments = new Fragment[]{new HomeFragment(), new MyOrderFragment(), new MyFragment()};
//        String[] titles = new String[]{"待审订单", "全部订单", "订单统计"};
        viewpager.setOffscreenPageLimit(3);
        viewpager.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) {
            @Override
            public Fragment getItem(int position) {
                return fragments[position];
            }

            @Override
            public int getCount() {
                return fragments.length;
            }


        });
//
        bottomView.setOnChooseListener(this);
        EventBus.getDefault().register(this);
    }

    public void getUser() {
        if (!Network.checkNetwork(this)) {
            ToastUtil.makeText(this, "网络连接失败").show();
            return;
        }

        Api.api_service.getUser().compose(RxLifeUtil.NocheckOn(this)).
                doFinally(() -> {
                    try {

                    } catch (NullPointerException e) {

                    }
                }).
                subscribe(result -> {
                    if (result.code.equals("00")) {
                        EventBus.getDefault().post(new UserEvent(result.datas));
                    } else {
                        EventBus.getDefault().post(new UserNoDataEvent());
                    }
                });
    }

    @Override
    protected void onResume() {
        super.onResume();
        getUser();
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEvent(ChangPageEvent event) {
        bottomView.setSelectPage(event.page);

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
        unbiner.unbind();
        unbiner = null;
    }

    private static long exitTime = 0;

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
            if (System.currentTimeMillis() - exitTime > 2000) {
                Toast.makeText(this,
                        "再按一次退出",
                        Toast.LENGTH_SHORT).show();
                exitTime = System.currentTimeMillis();
            } else {
                this.finish();
                System.exit(0);
            }
            return false;
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    public void onClick(int poi) {
        viewpager.setCurrentItem(poi);
    }
}
